package com.hongdee.crm.group.service.impl;

import com.hongdee.crm.group.dao.ITCrmGroupDao;
import com.hongdee.crm.group.entity.TCrmGroup;
import com.hongdee.crm.group.service.ITCrmGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by My on 2017/2/20.
 */
@Service
public class TCrmGroupServiceImpl implements ITCrmGroupService {
    private String tabName ="T_CRM_GROUP";
    private String entity ="TCrmGroup";
    @Autowired
    private ITCrmGroupDao itCrmGroupDao;
    /**
     * @param start 当前页面
     * @param limit 页面大小
     * @param oid 公司编号
     * @return Map
     * @throws Exception
     * @desc 分页查询
     * @author 徐钦勇
     */
    @Override
    public Map page (String start,String limit,String oid,String type) throws Exception{
        String sql="select * from "+tabName+ " where 1=1 ";
        List params = new ArrayList();
        if(!oid.isEmpty()){
            sql+=" and company_id= ? ";
            params.add(oid);
        }
        if(!StringUtils.isEmpty(type)){
            sql+=" and type = ? ";
            params.add(type);
        }
        return this.itCrmGroupDao.page(sql,params.toArray(),start,limit);
    }
    /**
     * @param id     主键
     * @param status 状态
     * @return Map
     * @throws Exception
     * @desc 启用停用
     * @author 徐钦勇
     */
    @Override
    public void update(String id, String status) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录");
        }
        String hql = "update " + entity + " set status = :status where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("status", status);
        this.itCrmGroupDao.update(hql, params);
    }
    /**
     * @param data TCrmGroup 实体类
     * @return Map
     * @throws Exception
     * @desc
     * @author 徐钦勇
     */
    @Override
    public TCrmGroup saveOrUpdate(TCrmGroup data) throws Exception{
        return (TCrmGroup)this.itCrmGroupDao.saveOrUpdate(data);
    }
    /**
     * @param id 主键
     * @return Map
     * @throws Exception
     * @desc   分组明细
     * @author 徐钦勇
     */
    @Override
    public Map info(String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择记录");
        }
        String sql = "select * from " + tabName + " where id = ? ";
        return this.itCrmGroupDao.info(sql, new Object[]{id});
    }
    /**
     * @param type
     * @return Map
     * @throws Exception
     * @desc   分组下拉
     * @author 徐钦勇
     */
    @Override
    public List<Map> combo(String type) throws Exception {
        String sql = " select * from " + tabName + " where status ='1' and type = ?  order by group_name";
        List<Map<String, Object>> mapList = this.itCrmGroupDao.list(sql, type);
        List<Map> resList = new ArrayList<Map>();

        for (Map map : mapList) {
            Map temp = new HashMap();
            temp.put("label", map.get("groupName"));
            temp.put("value", map.get("id"));
            resList.add(temp);
        }
        return resList;
    }
    /**
     * @return Map
     * @throws Exception
     * @desc 分组编码转换
     * @author 徐钦勇
     */
    @Override
    public Map convert() throws Exception {
        String sql = " select * from " + tabName + "  ";
        List<Map<String, Object>> mapList = this.itCrmGroupDao.list(sql);
        List<Map> resList = new ArrayList<Map>();
        Map temp = new HashMap();
        for (Map map : mapList) {
            temp.put(map.get("id"), map.get("groupName"));
        }
        return temp;
    }
}
